<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>设置我的资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/house/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/house/layuiadmin/style/admin.css" media="all">
    <link href="/house/static/layui/css/layui.css" rel="stylesheet">
    <script src="/house/static/layui/layui.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">设置我的资料</div>
                <div class="layui-card-body" pad15>
                    <div class="layui-form" lay-filter="userInfo">
                        <input type="hidden" name="id">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" value="" readonly class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickname" value="" lay-verify="nickname" autocomplete="off"
                                       placeholder="请输入姓名" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <select name="sex" lay-filter="sex">
                                    <option value="">性别</option>
                                    <option value="女">女</option>
                                    <option value="男">男</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" value="" lay-verify="phone" autocomplete="off"
                                       placeholder="请输入手机号" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="setMyInfo">确认修改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(function () {
        let form = layui.form;
        let $ = layui.$;
        let layer = layui.layer;

        //  1.获取用户的基本信息，回显用户基本资料

        //通过登录之后返回的用户信息，但是有弊端，如果管理员在用户登录之后将数据库中的用户信息修改，此时数据的回显就与数据库中的数据不一致。
        // let userinfo = JSON.parse(sessionStorage.getItem("userInfo"));
        // form.val('userInfo',userinfo);
        // form.render();

        //优化：发送异步请求，获取用户的基本信息，回显用户基本资料
        let username = sessionStorage.getItem("username");
        $.ajax({
            url:'/house/user/personalInfo',
            data:{username},    //{username:username}
            dataType:'JSON',
            type:'GET',
            success:function (res){
                if (res.code===0){
                    let user = res.data;
                    console.log(user);
                    form.val('userInfo',user);
                    form.render();
                }
            }
        })

        //监听表单事件 提交修改
        form.on('submit(setMyInfo)', function (obj) {
            let formData = obj.field;
            // layer.msg(JSON.stringify(formData));
            // 发送异步请求 实现用户个人信息修改
            $.post('/house/user/edit',formData,function (res){
                if (res.code===0){
                    layer.msg(res.message,{icon:1})
                }else{
                    layer.msg(res.message,{icon:2})
                }
            })
        })
    })
</script>
</body>
</html>